# 使用支持 aarch64 架构的基础镜像
FROM vllm-ascend:v0.11.0rc0

# 设置工作目录
WORKDIR /data/work/miniconda

# 定义安装路径
ENV MINICONDA_PATH=/data/work/miniconda
ENV INSTALL_PATH=/data/work/miniconda/install
ENV PATH="${INSTALL_PATH}/bin:${PATH}"

# 更新 apt 并安装必要的工具
RUN apt update && \
    apt install -y wget vim && \
    apt install -y curl && \
    apt install -y git && \
    apt install espeak -y && \
    apt install -y pkg-config && \
    apt install -y libsentencepiece-dev && \
    apt install -y libopenblas-dev && \
    apt install -y liblapack-dev && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

# 创建目录
RUN mkdir -p ${MINICONDA_PATH} && \
    cd ${MINICONDA_PATH}

# 下载 Miniconda 安装包（清华镜像，aarch64架构，py39版本）
RUN echo "正在下载 Miniconda 安装包..." && \
    wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py39_4.9.2-Linux-aarch64.sh && \
    echo "开始安装 Miniconda 到 ${INSTALL_PATH}..." && \
    bash Miniconda3-py39_4.9.2-Linux-aarch64.sh -b -p ${INSTALL_PATH} && \
    rm -f Miniconda3-py39_4.9.2-Linux-aarch64.sh

# 初始化 conda（写入 bashrc 配置）
RUN ${INSTALL_PATH}/bin/conda init bash

# 配置环境变量到 ~/.bashrc
RUN echo "" >> ~/.bashrc && \
    echo "# Miniconda 环境变量" >> ~/.bashrc && \
    echo "export PATH=\"${INSTALL_PATH}/bin:\$PATH\"" >> ~/.bashrc && \
    source ~/.bashrc

# 验证安装
RUN ${INSTALL_PATH}/bin/conda --version && \
    echo "Miniconda 安装完成"

# 创建 conda 环境
RUN ${INSTALL_PATH}/bin/conda create -n hf-npu python==3.10 -y

# 配置 pip 镜像源并安装依赖包
RUN ${INSTALL_PATH}/bin/conda run -n hf-npu pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

WORKDIR /data/work

# 激活 hf-npu 环境并安装依赖包
RUN conda run -n hf-npu pip install attrs && \
    conda run -n hf-npu pip install decorator>=5.1.1 && \
    conda run -n hf-npu pip install einops && \
    conda run -n hf-npu pip install fastapi>=0.104.0 && \
    conda run -n hf-npu pip install llama-cpp-python && \
    conda run -n hf-npu pip install pillow && \
    conda run -n hf-npu pip install protobuf && \
    conda run -n hf-npu pip install psutil && \
    conda run -n hf-npu pip install pydantic>=2.0.0 && \
    conda run -n hf-npu pip install python-multipart>=0.0.6 && \
    conda run -n hf-npu pip install scipy && \
    conda run -n hf-npu pip install soundfile==0.13.1 && \
    conda run -n hf-npu pip install sentencepiece && \
    conda run -n hf-npu pip install torch==2.8.0 && \
    conda run -n hf-npu pip install torch_npu==2.8.0 && \
    conda run -n hf-npu pip install torchvision==0.23.0 && \
    conda run -n hf-npu pip install transformers && \
    conda run -n hf-npu pip install uvicorn[standard]>=0.24.0 && \
    conda run -n hf-npu pip install librosa==0.11.0 && \
    conda run -n hf-npu pip install neucodec>=0.0.4 && \
    conda run -n hf-npu pip install phonemizer==3.3.0 && \
    conda run -n hf-npu pip install aiohttp && \
    conda run -n hf-npu pip install llama-cpp-python==0.3.16 && \
    conda run -n hf-npu pip install resemble-perth>=1.0.1 && \
    conda run -n hf-npu pip uninstall torch -y && \
    conda run -n hf-npu pip uninstall torchaudio -y && \
    conda run -n hf-npu pip install torch==2.8.0 && \
    conda run -n hf-npu pip install torchaudio==2.8.0 && \
    conda run -n hf-npu pip uninstall numpy -y && \
    conda run -n hf-npu pip install numpy==1.26.4


# 复制应用文件到容器
COPY ./app /data/work/app

# 创建 huggingface 缓存目录（使用绝对路径，因为 ~ 在 Dockerfile 中不会展开）
RUN mkdir -p /root/.cache/huggingface/

# 复制 huggingface hub 文件到容器（使用绝对路径）
COPY ./docker-huggingface/hub /root/.cache/huggingface/hub

# 设置工作目录到应用目录
WORKDIR /data/work/app

# 设置Ascend CANN库路径（如果挂载了宿主机目录，这些路径会自动生效）
# 注意：这些路径在运行时通过卷挂载从宿主机提供
# 包含所有可能的库路径，确保能找到 libascend_hal.so
ENV LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/stub:/usr/local/Ascend/add-ons:/usr/local/Ascend/driver/lib:$LD_LIBRARY_PATH
ENV ASCEND_RT_VISIBLE_DEVICES=0

# 激活 conda 环境并运行主程序
# 需要先初始化 conda，然后激活环境
CMD ["/bin/bash", "-c", "/data/develop/miniconda/install/envs/hf-npu/bin/python main.py"]

